package com.yckj.activechicken.business.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.yckj.activechicken.business.common.mapper.CustomSequenceMapper;
import com.yckj.activechicken.business.document.mapper.BusiDocumentMapper;
import com.yckj.activechicken.business.document.model.BusiDocument;
import com.yckj.activechicken.business.document.model.BusiDocumentExample;
import com.yckj.activechicken.business.goods.mapper.GoodsAttributeMapper;
import com.yckj.activechicken.business.goods.mapper.GoodsInfoMapper;
import com.yckj.activechicken.business.goods.mapper.comp.GoodsInfoMapperComp;
import com.yckj.activechicken.business.goods.model.GoodsAttribute;
import com.yckj.activechicken.business.goods.model.GoodsAttributeExample;
import com.yckj.activechicken.business.goods.model.GoodsInfo;
import com.yckj.activechicken.business.goods.model.GoodsInfoExample;
import com.yckj.activechicken.business.goods.model.po.RemoveFile;
import com.yckj.activechicken.business.goods.service.IGoodsService;
import com.yckj.activechicken.common.utils.StringUtils;
import com.yckj.activechicken.common.utils.file.FileUtils;
import com.yckj.activechicken.framework.config.ProjectConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Title:com.yckj.activechicken.business.goods.service.impl
 * </p>
 * <p/>
 * <p>
 * Description: 描述【货品管理业务处理实体】
 * </p>
 * <p/>
 * <p>
 * Copyright: Copyright (c) 2019
 * </p>
 * <p/>
 * <p>
 * Company: 英才汇硕信息技术有限公司
 * </p>
 *
 * @author zhoujy
 * @version 1.0
 * @created 2020/6/7 20:38
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    private BusiDocumentMapper busiDocumentMapper;

    @Autowired
    private CustomSequenceMapper customSequenceMapper;

    @Autowired
    private GoodsInfoMapperComp goodsInfoMapperComp;


    @Value("${upload.goodsImage}")
    private String goodsImage;

    /**
     * 添加货品信息
     * @param goodsInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, String> addGoods(GoodsInfo goodsInfo) {
        Map<String,String> outMap = new HashMap<>(2);

        //1、重名性校验
        GoodsInfoExample goodsInfoExample = new GoodsInfoExample();
        goodsInfoExample.createCriteria().andGoodsNameEqualTo(goodsInfo.getGoodsName());
        List<GoodsInfo> queryGoodsList = goodsInfoMapper.selectByExample(goodsInfoExample);
        if(StringUtils.isNotEmpty(queryGoodsList)){
            outMap.put("result","false");
            outMap.put("message","该货品已经存在，请确认！");
            return outMap;
        }

        //2、添加货品主要信息
        String goodsId = customSequenceMapper.selectNextVal("goods_id");
        goodsInfo.setGoodsId(goodsInfo.getGoodsCategory()+goodsId);
        goodsInfoMapper.insertSelective(goodsInfo);

        //3、添加货品属性信息
        List<GoodsAttribute> attributeList = goodsInfo.getGoodsAttributeList();
        if(StringUtils.isNotEmpty(attributeList)){
            for(GoodsAttribute goodsAttribute : attributeList){
                goodsAttribute.setGoodsId(goodsInfo.getGoodsId());
                goodsAttributeMapper.insertSelective(goodsAttribute);
            }
        }

        //4、处理上传图片信息
        List<Map<String,String>> outList =  FileUtils.uploadFileList(goodsInfo.getImageFile(),this.goodsImage);
        if(StringUtils.isNotEmpty(outList)){
            for(Map<String,String> map : outList){
                if("true".equals(map.get("result"))){
                    BusiDocument busiDocument = new BusiDocument();
                    busiDocument.setBusiId(goodsInfo.getGoodsId());
                    busiDocument.setDocName(map.get("fileName"));
                    busiDocument.setDocLocalName(map.get("localFileName"));
                    busiDocument.setDocMimeType(map.get("mimeType"));
                    busiDocument.setDocType("货品图片");
                    //对于需要在页面展示的图片需要加入/profile路径进行处理，使用profile代理
                    busiDocument.setDocUrl("/profile" + map.get("uploadPath"));
                    busiDocument.setRemark(goodsInfo.getGoodsName()+"的货品图片");
                    busiDocument.setCreateBy(goodsInfo.getCreateBy());
                    busiDocument.setCreateTime(goodsInfo.getCreateTime());
                    busiDocument.setUpdateBy(goodsInfo.getCreateBy());
                    busiDocument.setUpdateTime(goodsInfo.getUpdateTime());
                    busiDocumentMapper.insertSelective(busiDocument);
                }else{
                    outMap.put("result","false");
                    outMap.put("message","货品创建图片上传异常，请在修改功能中查看图片信息！");
                    return outMap;
                }
            }
        }

        outMap.put("result","true");
        return outMap;
    }

    /**
     * 修改货品信息
     * @param goodsInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, String> modifyGoods(GoodsInfo goodsInfo) {
        Map<String,String> outMap = new HashMap<>(2);

        //1、重名性校验
        String goodsName = goodsInfo.getGoodsName();
        GoodsInfoExample goodsInfoExample = new GoodsInfoExample();
        goodsInfoExample.createCriteria().andGoodsNameEqualTo(goodsName);
        List<GoodsInfo> goodsInfoList = goodsInfoMapper.selectByExample(goodsInfoExample);
        if(StringUtils.isNotEmpty(goodsInfoList)){
            if(!goodsInfoList.get(0).getGoodsId().equals(goodsInfo.getGoodsId())){
                outMap.put("result","false");
                outMap.put("message","该货品名已经存在，请确认！");
                return outMap;
            }
        }

        //2、修改货品信息
        goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);

        //3、修改货品属性信息
        List<GoodsAttribute> goodsAttributeList = goodsInfo.getGoodsAttributeList();
        if(StringUtils.isNotEmpty(goodsAttributeList)){
            for(GoodsAttribute goodsAttribute : goodsAttributeList){
                //修改信息
                if(goodsAttribute.getAttrId() != null){
                    goodsAttributeMapper.updateByPrimaryKeySelective(goodsAttribute);
                }else{
                    //添加信息
                    goodsAttribute.setGoodsId(goodsInfo.getGoodsId());
                    goodsAttributeMapper.insertSelective(goodsAttribute);
                }
            }
        }

        //4、删除旧图片
        List<RemoveFile> removeFileList = goodsInfo.getRemoveFileList();
        if(StringUtils.isNotEmpty(removeFileList)){
            for(RemoveFile removeFile : removeFileList){
                BusiDocumentExample busiDocumentExample = new BusiDocumentExample();
                busiDocumentExample.createCriteria().andDocNameEqualTo(removeFile.getDocName())
                        .andDocUrlEqualTo(removeFile.getDocUrl());
                busiDocumentMapper.deleteByExample(busiDocumentExample);
                String delPath = ProjectConfig.getProfile() + removeFile.getDocUrl();
                FileUtils.deleteFile(delPath);
            }
        }

        //5、添加新的图片
        List<Map<String,String>> outList =  FileUtils.uploadFileList(goodsInfo.getImageFile(),this.goodsImage);
        if(StringUtils.isNotEmpty(outList)){
            for(Map<String,String> map : outList){
                if("true".equals(map.get("result"))){
                    BusiDocument busiDocument = new BusiDocument();
                    busiDocument.setBusiId(goodsInfo.getGoodsId());
                    busiDocument.setDocName(map.get("fileName"));
                    busiDocument.setDocLocalName(map.get("localFileName"));
                    busiDocument.setDocMimeType(map.get("mimeType"));
                    busiDocument.setDocType("货品图片");
                    //对于需要在页面展示的图片需要加入/profile路径进行处理，使用profile代理
                    busiDocument.setDocUrl("/profile" + map.get("uploadPath"));
                    busiDocument.setRemark(goodsInfo.getGoodsName()+"的货品图片");
                    busiDocument.setCreateBy(goodsInfo.getUpdateBy());
                    busiDocument.setCreateTime(goodsInfo.getUpdateTime());
                    busiDocument.setUpdateBy(goodsInfo.getUpdateBy());
                    busiDocument.setUpdateTime(goodsInfo.getUpdateTime());
                    busiDocumentMapper.insertSelective(busiDocument);
                }else{
                    outMap.put("result","false");
                    outMap.put("message","货品创建图片上传异常，请在修改功能中查看图片信息！");
                    return outMap;
                }
            }
        }

        outMap.put("result","true");
        return outMap;
    }

    /**
     * 删除货品信息
     * @param goodsInfo
     * @return
     */
    @Override
    public int delGoods(GoodsInfo goodsInfo) {
        return goodsInfoMapper.updateByPrimaryKeySelective(goodsInfo);
    }

    /**
     * 分页查询货品信息
     * @param goodsInfo
     * @return
     */
    @Override
    public List<GoodsInfo> listGoods(GoodsInfo goodsInfo) {
        List<GoodsInfo> goodsInfoList = goodsInfoMapperComp.selectGoodsList(goodsInfo);

        GoodsAttributeExample attributeExample = new GoodsAttributeExample();

        if(StringUtils.isNotEmpty(goodsInfoList)){
            for(GoodsInfo outGoodsInfo : goodsInfoList){
                attributeExample.createCriteria().andGoodsIdEqualTo(outGoodsInfo.getGoodsId());
                List<GoodsAttribute> attributeList = goodsAttributeMapper.selectByExample(attributeExample);
                outGoodsInfo.setGoodsAttributeList(attributeList);
                outGoodsInfo.setGoodsAttrListJson(JSON.toJSONString(attributeList));
                attributeExample.clear();
            }
        }

        return goodsInfoList;
    }
}
